using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class Client
	{
		public int GetMaxId()
		{
			return Conn.GetMaxID("ClientID", "HL_Client");
		}
		public bool Exists(int ClientID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from HL_Client");
			stringBuilder.Append(" where ClientID=@ClientID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ClientID", SqlDbType.Int, 4)
			};
			array[0].Value = ClientID;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public int Add(HKB.Data.Model.Client model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into HL_Client(");
			stringBuilder.Append("ParentID,ClientNum,ClientType,ClientName,ShortedForm,Area,ClientField,ValueLevel,CreditLevel,Importance,ConnectionLevel,GroupID,SourceType,PeriodType,Remark,VisitTimes,LastVisitTime,CreateTime,UpdateTime,Owner,IsBusiness,LastRelationTime)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@ParentID,@ClientNum,@ClientType,@ClientName,@ShortedForm,@Area,@ClientField,@ValueLevel,@CreditLevel,@Importance,@ConnectionLevel,@GroupID,@SourceType,@PeriodType,@Remark,@VisitTimes,@LastVisitTime,@CreateTime,@UpdateTime,@Owner,@IsBusiness,@LastRelationTime)");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ParentID", SqlDbType.Int, 4),
				new SqlParameter("@ClientNum", SqlDbType.NVarChar, 20),
				new SqlParameter("@ClientType", SqlDbType.NVarChar, 50),
				new SqlParameter("@ClientName", SqlDbType.NVarChar, 100),
				new SqlParameter("@ShortedForm", SqlDbType.NVarChar, 20),
				new SqlParameter("@Area", SqlDbType.NVarChar, 100),
				new SqlParameter("@ClientField", SqlDbType.NVarChar, 50),
				new SqlParameter("@ValueLevel", SqlDbType.NVarChar, 50),
				new SqlParameter("@CreditLevel", SqlDbType.NVarChar, 50),
				new SqlParameter("@Importance", SqlDbType.NVarChar, 50),
				new SqlParameter("@ConnectionLevel", SqlDbType.NVarChar, 50),
				new SqlParameter("@GroupID", SqlDbType.NVarChar, 50),
				new SqlParameter("@SourceType", SqlDbType.NVarChar, 50),
				new SqlParameter("@PeriodType", SqlDbType.NVarChar, 50),
				new SqlParameter("@Remark", SqlDbType.NText),
				new SqlParameter("@VisitTimes", SqlDbType.Int, 4),
				new SqlParameter("@LastVisitTime", SqlDbType.DateTime),
				new SqlParameter("@CreateTime", SqlDbType.DateTime),
				new SqlParameter("@UpdateTime", SqlDbType.DateTime),
				new SqlParameter("@Owner", SqlDbType.Int),
				new SqlParameter("@IsBusiness", SqlDbType.VarChar, 20),
				new SqlParameter("@LastRelationTime", SqlDbType.DateTime)
			};
			array[0].Value = model.ParentID;
			array[1].Value = model.ClientNum;
			array[2].Value = model.ClientType;
			array[3].Value = model.ClientName;
			array[4].Value = model.ShortedForm;
			array[5].Value = model.Area;
			array[6].Value = model.ClientField;
			array[7].Value = model.ValueLevel;
			array[8].Value = model.CreditLevel;
			array[9].Value = model.Importance;
			array[10].Value = model.ConnectionLevel;
			array[11].Value = model.GroupID;
			array[12].Value = model.SourceType;
			array[13].Value = model.PeriodType;
			array[14].Value = model.Remark;
			array[15].Value = model.VisitTimes;
			array[16].Value = model.LastVisitTime;
			array[17].Value = model.CreateTime;
			array[18].Value = model.UpdateTime;
			array[19].Value = model.Owner;
			array[20].Value = model.IsBusiness;
			array[21].Value = model.LastRelationTime;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public bool Update(HKB.Data.Model.Client model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update HL_Client set ");
			stringBuilder.Append("ParentID=@ParentID");
			if (!string.IsNullOrEmpty(model.ClientNum))
			{
				stringBuilder.Append(",ClientNum='" + model.ClientNum + "'");
			}
			if (!string.IsNullOrEmpty(model.ClientType))
			{
				stringBuilder.Append(",ClientType='" + model.ClientType + "'");
			}
			if (!string.IsNullOrEmpty(model.ClientName))
			{
				stringBuilder.Append(",ClientName='" + model.ClientName + "'");
			}
			if (!string.IsNullOrEmpty(model.ShortedForm))
			{
				stringBuilder.Append(",ShortedForm='" + model.ShortedForm + "'");
			}
			if (!string.IsNullOrEmpty(model.Area))
			{
				stringBuilder.Append(",Area='" + model.Area + "'");
			}
			if (!string.IsNullOrEmpty(model.ClientField))
			{
				stringBuilder.Append(",ClientField='" + model.ClientField + "'");
			}
			if (!string.IsNullOrEmpty(model.ValueLevel))
			{
				stringBuilder.Append(",ValueLevel='" + model.ValueLevel + "'");
			}
			if (!string.IsNullOrEmpty(model.CreditLevel))
			{
				stringBuilder.Append(",CreditLevel='" + model.CreditLevel + "'");
			}
			if (!string.IsNullOrEmpty(model.Importance))
			{
				stringBuilder.Append(",Importance='" + model.Importance + "'");
			}
			if (!string.IsNullOrEmpty(model.ConnectionLevel))
			{
				stringBuilder.Append(",ConnectionLevel='" + model.ConnectionLevel + "'");
			}
			if (!string.IsNullOrEmpty(model.GroupID))
			{
				stringBuilder.Append(",GroupID='" + model.GroupID + "'");
			}
			if (!string.IsNullOrEmpty(model.SourceType))
			{
				stringBuilder.Append(",SourceType='" + model.SourceType + "'");
			}
			if (!string.IsNullOrEmpty(model.PeriodType))
			{
				stringBuilder.Append(",PeriodType='" + model.PeriodType + "'");
			}
			if (!string.IsNullOrEmpty(model.Remark))
			{
				stringBuilder.Append(",Remark='" + model.Remark + "'");
			}
			if (!string.IsNullOrEmpty(model.VisitTimes.ToString()))
			{
				stringBuilder.Append(",VisitTimes=" + model.VisitTimes);
			}
			if (model.LastVisitTime.HasValue)
			{
				stringBuilder.Append(",LastVisitTime='" + model.LastVisitTime + "'");
			}
			if (model.UpdateTime.HasValue)
			{
				stringBuilder.Append(",UpdateTime='" + model.UpdateTime + "'");
			}
			if (model.LastRelationTime.HasValue)
			{
				stringBuilder.Append(",LastRelationTime='" + model.LastRelationTime + "'");
			}
			if (!string.IsNullOrEmpty(model.Owner.ToString()))
			{
				stringBuilder.Append(",Owner=" + model.Owner);
			}
			if (!string.IsNullOrEmpty(model.IsBusiness))
			{
				stringBuilder.Append(",IsBusiness='" + model.IsBusiness + "'");
			}
			stringBuilder.Append(" where ClientID=@ClientID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ParentID", SqlDbType.Int, 4),
				new SqlParameter("@ClientID", SqlDbType.Int, 4)
			};
			array[0].Value = model.ParentID;
			array[1].Value = model.ClientID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(int ClientID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HL_Client ");
			stringBuilder.Append(" where ClientID=@ClientID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ClientID", SqlDbType.Int, 4)
			};
			array[0].Value = ClientID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string ClientIDlist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HL_Client ");
			stringBuilder.Append(" where ClientID in (" + ClientIDlist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public HKB.Data.Model.Client GetModel(int ClientID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 * from HL_Client ");
			stringBuilder.Append(" where ClientID=@ClientID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ClientID", SqlDbType.Int, 4)
			};
			array[0].Value = ClientID;
			HKB.Data.Model.Client client = new HKB.Data.Model.Client();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			HKB.Data.Model.Client result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public HKB.Data.Model.Client DataRowToModel(DataRow row)
		{
			HKB.Data.Model.Client client = new HKB.Data.Model.Client();
			if (row != null)
			{
				if (row["ClientID"] != null && row["ClientID"].ToString() != "")
				{
					client.ClientID = int.Parse(row["ClientID"].ToString());
				}
				if (row["ParentID"] != null && row["ParentID"].ToString() != "")
				{
					client.ParentID = new int?(int.Parse(row["ParentID"].ToString()));
				}
				if (row["ClientNum"] != null)
				{
					client.ClientNum = row["ClientNum"].ToString();
				}
				if (row["ClientType"] != null)
				{
					client.ClientType = row["ClientType"].ToString();
				}
				if (row["ClientName"] != null)
				{
					client.ClientName = row["ClientName"].ToString();
				}
				if (row["ShortedForm"] != null)
				{
					client.ShortedForm = row["ShortedForm"].ToString();
				}
				if (row["Area"] != null)
				{
					client.Area = row["Area"].ToString();
				}
				if (row["ClientField"] != null)
				{
					client.ClientField = row["ClientField"].ToString();
				}
				if (row["ValueLevel"] != null)
				{
					client.ValueLevel = row["ValueLevel"].ToString();
				}
				if (row["CreditLevel"] != null)
				{
					client.CreditLevel = row["CreditLevel"].ToString();
				}
				if (row["Importance"] != null)
				{
					client.Importance = row["Importance"].ToString();
				}
				if (row["ConnectionLevel"] != null)
				{
					client.ConnectionLevel = row["ConnectionLevel"].ToString();
				}
				if (row["GroupID"] != null)
				{
					client.GroupID = row["GroupID"].ToString();
				}
				if (row["SourceType"] != null)
				{
					client.SourceType = row["SourceType"].ToString();
				}
				if (row["PeriodType"] != null)
				{
					client.PeriodType = row["PeriodType"].ToString();
				}
				if (row["Remark"] != null)
				{
					client.Remark = row["Remark"].ToString();
				}
				if (row["VisitTimes"] != null && row["VisitTimes"].ToString() != "")
				{
					client.VisitTimes = new int?(int.Parse(row["VisitTimes"].ToString()));
				}
				if (row["LastVisitTime"] != null && row["LastVisitTime"].ToString() != "")
				{
					client.LastVisitTime = new System.DateTime?(System.DateTime.Parse(row["LastVisitTime"].ToString()));
				}
				if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
				{
					client.CreateTime = new System.DateTime?(System.DateTime.Parse(row["CreateTime"].ToString()));
				}
				if (row["UpdateTime"] != null && row["UpdateTime"].ToString() != "")
				{
					client.UpdateTime = new System.DateTime?(System.DateTime.Parse(row["UpdateTime"].ToString()));
				}
				if (row["LastRelationTime"] != System.DBNull.Value)
				{
					client.UpdateTime = new System.DateTime?(System.DateTime.Parse(row["LastRelationTime"].ToString()));
				}
				if (row["Owner"] != null)
				{
					client.Owner = new int?(int.Parse(row["Owner"].ToString()));
				}
				if (row["IsBusiness"] != null)
				{
					client.IsBusiness = row["IsBusiness"].ToString();
				}
			}
			return client;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select * ");
			stringBuilder.Append(" FROM HL_Client ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" * FROM HL_Client ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + filedOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public int GetRecordCount(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) FROM HL_Client ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			object single = DbHelperSQL.GetSingle(stringBuilder.ToString());
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT * FROM ( ");
			stringBuilder.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				stringBuilder.Append("order by T." + orderby);
			}
			else
			{
				stringBuilder.Append("order by T.ClientID desc");
			}
			stringBuilder.Append(")AS Row, T.*  from HL_Client T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				stringBuilder.Append(" WHERE " + strWhere);
			}
			stringBuilder.Append(" ) TT");
			stringBuilder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public HKB.Data.Model.Client GetClientByClientName(string clientName, int owner)
		{
			HKB.Data.Model.Client result = null;
			string text = string.Concat(new object[]
			{
				"select * from HL_Client where ClientName='",
				clientName,
				"' and Owner=",
				owner
			});
			DataSet dataSet = Conn.Query(text.ToString(), new IDbDataParameter[0]);
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			return result;
		}
	}
}
